//
//  YGFoodGuideViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/13.
//

#import "YGFoodGuideViewController.h"
#import "YGFoodGuideTypeModel.h"
#import "YGHomeBLL.h"
#import "WJItemsControlView.h"
@interface YGFoodGuideViewController ()

@property (nonatomic, strong) NSArray <YGFoodGuideTypeModel *> *typeArray;      //一级分类
@property (nonatomic, strong) NSArray <YGFoodGuideTypeModel *> *lowerArray;    //二级分类

@property (nonatomic, strong) WJItemsControlView  *itemView;
@property (nonatomic, assign) NSInteger  itemSelectIndex;

@property (nonatomic, strong) WJItemsControlView  *lowerItemView;
@property (nonatomic, assign) NSInteger  lowerSelectIndex;

@property (nonatomic, strong) NSMutableArray *typeArrayTitle;      //一级分类标题
@property (nonatomic, strong) NSMutableArray *lowerArrayTitle;    //二级分类标题

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation YGFoodGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"饮食指南"];
    //设置导航栏属性
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xffffff)}];
    
     _typeArray = [NSMutableArray array];
     _lowerArray = [NSMutableArray array];
    
    self.typeArrayTitle = [NSMutableArray array];
    self.lowerArrayTitle = [NSMutableArray array];

    [self initView];
    [self initconstraints];
    
    [self requestTypeData];
}

-(void)initView{
    [self.view addSubview:self.itemView];
    [self.view addSubview:self.lowerItemView];
    [self.view addSubview:self.webView];
}

-(void)initconstraints{
    
    
}

- (void)requestTypeData{
    WeakSelf(self);
    [YGHomeBLL requestFoodGuideTypeInfoSuccess:^(NSArray<YGFoodGuideTypeModel *> * _Nonnull model) {
        weakself.typeArray = model;
        for (YGFoodGuideTypeModel *dmModel in model) {
            [weakself.typeArrayTitle addObject:dmModel.name];
        }
        weakself.itemView.titleArray = [weakself.typeArrayTitle copy];
        
        //默认选第一个
        YGFoodGuideTypeModel *yypeModel = [weakself.typeArray objectAtIndex:0];
        [weakself requestParentId:yypeModel.id];

    } fail:^(NSError * _Nonnull error) {
        
    }];
    
}

- (void)requestParentId:(NSString *)parentId{
    WeakSelf(self);
     [YGHomeBLL requestFoodGuideLowerWithParentId:parentId Success:^(NSArray<YGFoodGuideTypeModel *> * _Nonnull model) {
        weakself.lowerArray = model;
         [weakself.lowerArrayTitle removeAllObjects];
        for (YGFoodGuideTypeModel *dmModel in model) {
            [weakself.lowerArrayTitle addObject:dmModel.name];
        }
        weakself.lowerItemView.titleArray = [weakself.lowerArrayTitle copy];
       //默认选第一个
        YGFoodGuideTypeModel *typeModel = [weakself.lowerArray objectAtIndex:0];
        [weakself requestContentWithParentId:typeModel.id];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)requestContentWithParentId:(NSString *)parentId{
    WeakSelf(self);
    [YGHomeBLL requestFoodGuideContentWithTypeId:parentId Success:^(YGFoodGuideContentModel * _Nonnull model) {
        //webview展示h5富文本
        [weakself.webView loadHTMLString:model.content baseURL:nil];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}



#pragma setter
- (WJItemsControlView *)itemView{
    if (!_itemView) {
        WJItemsConfig *config = [[WJItemsConfig alloc]init];
        CGFloat width = self.view.width;
        config.textColor = UIColorFromRGB(0x999999);
        config.selectedColor = UIColorFromRGB(0x37C8D7);
        config.linePercent = 1;
       
        
        _itemView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0,kNavigationHeight, width, 44)];
        _itemView.config = config;
        WeakSelf(self);
//        [_itemView endMoveToIndex:self.itemSelectIndex];
        _itemView.tapItemWithIndex = ^(NSInteger index, BOOL animation) {
            YGFoodGuideTypeModel *model = [weakself.typeArray objectAtIndex:index];
            [weakself requestParentId:model.id];
        };
    }
    return _itemView;
}


- (WJItemsControlView *)lowerItemView{
    if (!_lowerItemView) {
        WJItemsConfig *config = [[WJItemsConfig alloc]init];
        config.textColor = UIColorFromRGB(0x999999);
        config.selectedColor = UIColorFromRGB(0x37C8D7);
        config.lineHieght = 0;
        config.linePercent = 0;
        CGFloat width = self.view.width;
        _lowerItemView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0,self.itemView.bottom, width, 44)];
        _lowerItemView.config = config;
//        [_lowerItemView endMoveToIndex:self.lowerSelectIndex];
        WeakSelf(self);
        _lowerItemView.tapItemWithIndex = ^(NSInteger index, BOOL animation) {
            YGFoodGuideTypeModel *model = [weakself.lowerArray objectAtIndex:index];
            [weakself requestContentWithParentId:model.id];
        };
    }
    return _lowerItemView;
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.lowerItemView.bottom, self.view.width, self.view.height - self.lowerItemView.bottom)];
        _webView.delegate = self;
        [self.view addSubview:_webView];
    }
    return _webView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
